home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 295_01 / bputbf.c < prev    next >
Text File  |  1989-12-28  |  4KB  |  158 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bputbf.c    1.2 - 89/10/31" */
  5.  
  6. #include <errno.h>
  7. /*#include <stddef.h>*/
  8. /*#include <string.h>*/
  9. #include "blkio_.h"
  10.  
  11. /*man---------------------------------------------------------------------------
  12. NAME
  13.      bputbf - put a block field into a block file
  14.  
  15. SYNOPSIS
  16.      #include <blkio.h>
  17.  
  18.      int bputbf(bp, bn, offset, buf, bufsize)
  19.      BLKFILE *bp;
  20.      bpos_t bn;
  21.      size_t offset;
  22.      const void *buf;
  23.      size_t bufsize;
  24.  
  25. DESCRIPTION
  26.      The bputbf function writes the contents of buf into a field of
  27.      block number bn of the block file associated with BLKFILE pointer
  28.      bp.  The field begins offset characters from the beginning of the
  29.      block and is bufsize characters long.  buf must point to a
  30.      storage area at least bufsize characters long.  Block numbering
  31.      starts at 1.
  32.  
  33.      bputbf will fail if one or more of the following is true:
  34.  
  35.      [EINVAL]       bp is not a valid BLKFILE pointer.
  36.      [EINVAL]       bn or bufsize is less than 1.
  37.      [EINVAL]       buf is the NULL pointer.
  38.      [BEBOUND]      offset + bufsize extends beyond the
  39.                     boundary of block bn.
  40.      [BEEOF]        Partial block being written and block
  41.                     bn is past the end of file.
  42.      [BEEOF]        Complete block being written and block
  43.                     bn is more than 1 past the end of file.
  44.      [BENOPEN]      bp is not open for writing.
  45.  
  46. SEE ALSO
  47.      bgetbf, bputb, bputhf.
  48.  
  49. DIAGNOSTICS
  50.      Upon successful completion, a value of 0 is returned.  Otherwise,
  51.      a value of -1 is returned, and errno set to indicate the error.
  52.  
  53. NOTES
  54.      Whenever a new block is created at the end of the file, the
  55.      entire block must be written, i.e., offset must have a value of 0
  56.      and bufsize must be equal to the block size.
  57.  
  58. ------------------------------------------------------------------------------*/
  59. int bputbf(bp, bn, offset, buf, bufsize)
  60. BLKFILE *bp;
  61. bpos_t bn;
  62. size_t offset;
  63. CONST void *buf;
  64. size_t bufsize;
  65. {
  66.     int i = 0;
  67.     size_t bufno = 0;
  68.  
  69.     /* validate arguments */
  70.     if (!b_valid(bp) || (bn < 1) || (buf == NULL) || (bufsize < 1)) {
  71.         errno = EINVAL;
  72.         return -1;
  73.     }
  74.  
  75.     /* check if not open for writing */
  76.     if (!(bp->flags & BIOWRITE)) {
  77.         errno = BENOPEN;
  78.         return -1;
  79.     }
  80.  
  81.     /* check if block boundary is crossed */
  82.     if ((offset + bufsize) > bp->blksize) {
  83.         errno = BEBOUND;
  84.         return -1;
  85.     }
  86.  
  87.     /* check if past end of file */
  88.     if ((offset != 0) || (bufsize != bp->blksize)) {
  89.         if (bn >= bp->endblk) {
  90.             errno = BEEOF;
  91.             return -1;
  92.         }
  93.     } else {
  94.         if (bn > bp->endblk) {
  95.             errno = BEEOF;
  96.             return -1;
  97.         }
  98.     }
  99.  
  100.     /* check if not buffered */
  101.     if (bp->bufcnt == 0) {
  102.         if (b_uputf(bp, bn, offset, buf, bufsize) == -1) {
  103.             BEPRINT;
  104.             return -1;
  105.         }
  106.         if (bp->endblk <= bn) {
  107.             bp->endblk = bn + 1;
  108.         }
  109.         errno = 0;
  110.         return 0;
  111.     }
  112.  
  113.     /* search buffer list for block */
  114.     for (i = 1; i <= bp->bufcnt; i++) {
  115.         if ((b_block_p(bp, (size_t)i)->bn == bn)
  116.                    && (b_block_p(bp, (size_t)i)->flags & BLKREAD)) {
  117.             bufno = i;
  118.             break;
  119.         }
  120.     }
  121.  
  122.     /* if not found, use least recently used buffer */
  123.     if (bufno == 0) {
  124.         bufno = bp->least;
  125.         if (b_put(bp, bufno) == -1) {    /* flush previous contents */
  126.             BEPRINT;
  127.             return -1;
  128.         }
  129.         b_block_p(bp, bufno)->flags = 0;
  130.         b_block_p(bp, bufno)->bn = bn;
  131.         if ((offset != 0) || (bufsize != bp->blksize)) {
  132.             /* read block from file */
  133.             if (b_get(bp, bufno) == -1) {
  134.                 if (errno != BEEOF) BEPRINT;
  135.                 return -1;
  136.             }
  137.         }
  138.     }
  139.  
  140.     /* copy from buf into block buffer and set flags */
  141.     memcpy(((char *)b_blkbuf(bp, bufno) + offset), buf, bufsize);
  142.     b_block_p(bp, bufno)->flags = BLKREAD | BLKWRITE;
  143.  
  144.     /* adjust endblk */
  145.     if (bp->endblk <= bn) {
  146.         bp->endblk = bn + 1;
  147.     }
  148.  
  149.     /* move block buffer bufno to most recently used end of list */
  150.     if (b_mkmru(bp, bufno) == -1) {
  151.         BEPRINT;
  152.         return -1;
  153.     }
  154.  
  155.     errno = 0;
  156.     return 0;
  157. }
  158.